package com.rockxin.cat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.MainActivity;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.UseBean;
import com.rockxin.cat.bean.UserBean;
import com.rockxin.cat.event.LoginEvent;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;
import com.rockxin.cat.utils.ToastUtils;

import org.simple.eventbus.EventBus;

import rx.Subscriber;

public class SplashActivity extends AppCompatActivity {
    private SPUtils spUtils;
    private SplashActivity mSplashActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mSplashActivity = this;


        initData();

//        Intent intent = new Intent(mSplashActivity, LendDetailsActivity.class);
//        startActivity(intent);
//        finish();
    }


    private void initData() {
        spUtils = new SPUtils("Guide");

    }


    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean first = spUtils.getBoolean("first", true);
                if (first) {
                    Intent intent = new Intent(mSplashActivity, GuidePageActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    login();
                }
            }

        }, 2000);
    }

    private void login() {
        final String phone = spUtils.getString(Constant.PREFERENCES.PHONE);
        final String passwd = spUtils.getString(Constant.PREFERENCES.PASSWORD);
        Logger.d("phone:" + phone + "      " + (phone != null));
        if (phone != null && passwd != null) {
            final CatNetWork catNetWork = new CatNetWork();
            catNetWork.login(phone, passwd, new Subscriber<HttpResult<UseBean>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }

                @Override
                public void onNext(HttpResult<UseBean> useBeanHttpResult) {
                    Logger.json(new Gson().toJson(useBeanHttpResult));
                    SPUtils spUtils = new SPUtils("Guide");
                    final UserBean userInfo = new UserBean();
                    if (useBeanHttpResult.getStatus() == 1) {
                        spUtils.put(Constant.PREFERENCES.PHONE, phone);
                        spUtils.put(Constant.PREFERENCES.PASSWORD, passwd);
                        spUtils.put(Constant.PREFERENCES.TOKEN, useBeanHttpResult.getData().getToken());
                        userInfo.setToken(useBeanHttpResult.getData().getToken());
                        User.getInstance().saveUserInfo(userInfo);
                    
                        EventBus.getDefault().post(new LoginEvent(useBeanHttpResult.getData().getToken()), "login");
                    } else {
                        spUtils.put(Constant.PREFERENCES.PHONE, "");
                        spUtils.put(Constant.PREFERENCES.PASSWORD, "");
                        spUtils.put(Constant.PREFERENCES.TOKEN, "");
                        User.getInstance().clearUserInfo();
                    }
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
            });
        } else {
            Logger.d("未登录");
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }

    }
}
